/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 20:36:40
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-11-13 13:42:22
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/src/xict_ui_common/utils/config.cpp
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#include "xict_ui_common/utils/config.h"
#include <QDebug>
#include <QFile>

namespace xict_ui_common {

    bool Config::Init(const QString& cfg_path) {
        QFile file;
        file.setFileName(cfg_path);
        if (!file.exists()) {
            qDebug() << "error  config file not exist" << cfg_path;
            return false;
        }
        // m_psetting = new QSettings(cfg_path, QSettings::IniFormat);
        m_config = YAML::LoadFile(cfg_path.toStdString());
        return true;
    }

    void Config::Set(QString qstrNodeName, QString qstrKeyName,
                     QVariant qvarvalue) {
        m_psetting->setValue(
            QString("/%1/%2").arg(qstrNodeName).arg(qstrKeyName), qvarvalue);
    }

    QVariant Config::GetCfg(QString qstrNodeName, QString qstrKeyName) {
        QVariant qvar = m_psetting->value(
            QString("/%1/%2").arg(qstrNodeName).arg(qstrKeyName));
        return qvar;
    }

    std::string Config::GetYaml(QString qstrNodeName, QString qstrKeyName) {
        YAML::Node topic_config = m_config[qstrNodeName.toStdString()];
        return topic_config[qstrKeyName.toStdString()].as<std::string>();
    }

    bool Config::GetJsonArray(const QString& json_path, QJsonArray& out_array) {
        QFile loadFile(json_path);
        if (!loadFile.open(QIODevice::ReadOnly)) {
            return false;
        }

        QByteArray allData = loadFile.readAll();
        loadFile.close();

        QJsonParseError json_error;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));

        if (json_error.error != QJsonParseError::NoError) {
            return false;
        }
        out_array = jsonDoc.array();
        return true;
    }

    bool Config::GetYamlArray(const QString& yaml_path, QJsonArray& out_array) {
        // Load YAML file
        YAML::Node config = YAML::LoadFile(yaml_path.toStdString());

        // Iterate over each entry in the YAML file
        for (const auto& vehicle : config["vehicle_list"]) {
            QJsonObject vehicleObj;
            vehicleObj["name"]  = vehicle["name"].as<std::string>().c_str();
            vehicleObj["host"]  = vehicle["host"].as<std::string>().c_str();
            vehicleObj["state"] = vehicle["state"].as<std::string>().c_str();
            vehicleObj["describe"] =
                vehicle["describe"].as<std::string>().c_str();
            vehicleObj["type"] = vehicle["type"].as<std::string>().c_str();

            QJsonArray sceneArray;
            for (const auto& scene : vehicle["scene_list"]) {
                sceneArray.append(scene.as<std::string>().c_str());
            }

            vehicleObj["scene_list"] = sceneArray;

            // Append the QJsonObject to the QJsonArray
            out_array.append(vehicleObj);
        }

        // Output the resulting QJsonArray
        QJsonDocument doc(out_array);
        return true;
    }
}   // namespace xict_ui_common
